package ui

import (
	"bytes"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"log"
	"strings"
)

var fontMask image.Image

func init() {
	im, err := png.Decode(bytes.NewBuffer(fontData))
	if err != nil {
		log.Fatalln(err)
	}
	size := im.Bounds().Size()
	mask := image.NewRGBA(im.Bounds())
	for y := 0; y < size.Y; y++ {
		for x := 0; x < size.X; x++ {
			r, _, _, _ := im.At(x, y).RGBA()
			if r > 0 {
				mask.Set(x, y, color.Opaque)
			}
		}
	}
	fontMask = mask
}

func CreateGenericThumbnail(text string) image.Image {
	im := image.NewRGBA(image.Rect(0, 0, 256, 240))
	draw.Draw(im, im.Rect, &image.Uniform{color.Black}, image.ZP, draw.Src)
	DrawCenteredText(im, text, 1, 2, color.RGBA{128, 128, 128, 255})
	DrawCenteredText(im, text, 0, 0, color.White)
	return im
}

func WordWrap(text string, maxLength int) []string {
	var rows []string
	words := strings.Fields(text)
	if len(words) == 0 {
		return rows
	}
	row := words[0]
	for _, word := range words[1:] {
		newRow := row + " " + word
		if len(newRow) <= maxLength {
			row = newRow
		} else {
			rows = append(rows, row)
			row = word
		}
	}
	rows = append(rows, row)
	return rows
}

func DrawCenteredText(dst draw.Image, text string, dx, dy int, c color.Color) {
	rows := WordWrap(text, 15)
	for i, row := range rows {
		x := 128 - len(row)*8
		y := 120 - len(rows)*12 + i*24
		DrawText(dst, x+dx, y+dy, row, c)
	}
}

func DrawCharacter(dst draw.Image, x, y int, ch byte, c color.Color) {
	if ch < 32 || ch > 128 {
		return
	}
	cx := int((ch-32)%16) * 16
	cy := int((ch-32)/16) * 16
	r := image.Rect(x, y, x+16, y+16)
	src := &image.Uniform{c}
	sp := image.Pt(cx, cy)
	draw.DrawMask(dst, r, src, sp, fontMask, sp, draw.Over)
}

func DrawText(dst draw.Image, x, y int, text string, c color.Color) {
	for i := range text {
		DrawCharacter(dst, x, y, text[i], c)
		x += 16
	}
}

var fontData = []byte{
	0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
	0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x60,
	0x02, 0x03, 0x00, 0x00, 0x00, 0x8F, 0x9F, 0x44, 0x1B, 0x00, 0x00, 0x00,
	0x06, 0x50, 0x4C, 0x54, 0x45, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFE, 0x6A,
	0x62, 0xC8, 0x2E, 0x00, 0x00, 0x00, 0x01, 0x62, 0x4B, 0x47, 0x44, 0x00,
	0x88, 0x05, 0x1D, 0x48, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73,
	0x00, 0x00, 0x0E, 0xC4, 0x00, 0x00, 0x0E, 0xC4, 0x01, 0x95, 0x2B, 0x0E,
	0x1B, 0x00, 0x00, 0x02, 0xD2, 0x49, 0x44, 0x41, 0x54, 0x78, 0xDA, 0xC5,
	0x98, 0x49, 0x62, 0x23, 0x21, 0x0C, 0x45, 0xB5, 0xE1, 0x7E, 0xDA, 0xFC,
	0xFB, 0x5F, 0xA5, 0x4B, 0xE8, 0x4B, 0x88, 0x72, 0x2A, 0x1D, 0x07, 0xB9,
	0x9B, 0xC4, 0x2E, 0x8C, 0xE1, 0x19, 0x8D, 0x0C, 0x22, 0x56, 0x86, 0x5E,
	0xFF, 0x98, 0x2F, 0x81, 0x78, 0x81, 0xB5, 0xC5, 0x77, 0xF2, 0x50, 0xE2,
	0x9B, 0x63, 0x00, 0x90, 0x80, 0x71, 0xD5, 0xAD, 0xE6, 0x9F, 0xC5, 0x81,
	0x90, 0xD9, 0x92, 0xF0, 0x2C, 0xF9, 0x6B, 0x4D, 0x00, 0x00, 0xEA, 0xFD,
	0x31, 0x45, 0x40, 0x4C, 0xD2, 0xDE, 0x50, 0xC4, 0xBB, 0x4B, 0xD0, 0x02,
	0xF0, 0xCF, 0x26, 0x82, 0xBA, 0xE2, 0xE0, 0xDF, 0x06, 0x00, 0x5E, 0xD8,
	0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